<?php
namespace Common\Model;
use Think\Model;
/**
 * 基础model
 */
class BaseModel extends Model
{

    /**
     * [添加数据]
     * @method addData
     * @auther 大业
     * @create 2016-10-01
     * @param  [array] $data [新增数据]
     * @return [int/boolean] $id [id/false]
     */
    public function addData($data)
    {
        // 去除键值首尾的空格
        foreach ($data as $k => $v) {
            $data[$k] = trim($v);
        }
        $id = $this->add($data);
        return $id;
    }

    /**
     * [修改数据]
     * @method editData
     * @auther 大业
     * @create 2016-10-01
     * @param  [array] $where [查询条件]
     * @param  [array] $data [修改数据]
     * @return [int/boolean] $result [id/false]
     */
    public function editData($where,$data)
    {
   		if (empty($where)) {
            die('where条件为空为危险操作,不允许直接执行');
        }
        // 去除键值首尾的空格
        foreach ($data as $k => $v) {
        	$data[$k] = trim($v);
        }
        $result = $this
                ->where($where)
                ->save($data);
        return $result;
    }

    /**
     * [删除数据]
     * @method deleteData
     * @auther 大业
     * @create 2016-10-01
     * @param  [type] $where [查询条件]
     * @return [int/boolean] $result [id/false]
     */
    public function deleteData($where)
    {
        if (empty($where)) {
            die('where条件为空为危险操作,不允许直接执行');
        }
        $result = $this
                ->where($where)
                ->delete();
        return $result;
    }

    /**
     * [查询数据:查询列表中的第一条数据]
     * @method findData
     * @auther 大业
     * @create 2016-10-01
     * @param  [string] $where [查询条件]
     * @param  [string] $field [查询字段]
     * @param  [string] $sort [排序条件]
     * @return [array] [description]
     */
    public function findData($where,$field,$sort)
    {
    	if (empty($where)) {
    		$where = '1 = 1';
    	}
    	if(empty($field)){
    		$field = "*";
    	}
    	if(!empty($sort)){
    		$order = $sort;
    	}
    	$result = $this->field($field)
                ->where($where)
                ->find();
    	return $result;
    }

    /**
     * [查询数据:获取所有数据]
     * @method selectData
     * @author 大业
     * @create 2016-08-16
     * @param  [array] $where [查询条件]
     * @param  [string] $field [查询字段]
     * @param  [string] $field [排序条件]
     * @param  [array] $pageset [分页信息:startNum,endNum,limit]
     * @return [array] [查询信息]
     */
    public function selectData($where='1=1',$field='*',$order,$pageset)
    {
        //不需要分页信息时，正常查询
        if(empty($pageset)){
            $result = $this->field($field)
                    ->where($where)
                    ->order($order)
                    ->select();
        }else{
            //根据传入的开始和结束值查询，分页情况
            if(isset($pageset['startNum'])){
                echo "startNum......";
                $result = $this
                        ->field($field)
                        ->where($where)
                        ->limit($startNum,$endNum)
                        ->order($order)
                        ->select();
            }else{
                //根据传入的每页显示值，查具体分页信息，并返回分页样式.
                $count = $this->countData($where);
                $limit=empty($pageset['limit'])?$pageset['endNum']:$pageset['limit'];
                $pages= new \Org\Nx\Page($count,$limit);
                $pages->setConfig('first','首页');
                $pages->setConfig('prev','上一页');
                $pages->setConfig('next','下一页');
                $pages->setConfig('end','末页');
                $data = $this
                        ->field($field)
                        ->where($where)
                        ->limit($pages->firstRow.','.$pages->listRows)
                        ->order($order)
                        ->select();

                $result = array(
                    'list'=>$data,
                    'page'=>$pages->show()
                );
            }
        }
        return $result;
    }

    /**
     * [统计数据]
     * @method countData
     * @author 大业
     * @create 2016-08-16
     * @param  [string] $where [查询条件]
     * @return [type] [description]
     */
    public function countData($where)
    {
        if(empty($where)){
            $cnt = $this->count();
        }else{
            $cnt = $this->where($where)->count();
        }
        return $cnt;
    }

}
